home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGofer 0.22d / MacGofer Sources / mac_trap.a < prev    next >
Encoding:
Text File  |  1994-01-06  |  6.3 KB  |  241 lines  |  [TEXT/MPS ]

  1. ;*******************************************************************************
  2. ;
  3. ;        Self-modifying code to allow Gofer to
  4. ;        call Macintosh Toolbox routines.
  5.  
  6. ;        Copyright (c), Kevin Hammond, Glasgow University, 1992/3.
  7.  
  8. ;        The basic routine is:
  9. ;
  10. ;            TOOLBOXTRAP  trap retsize argcount args  
  11.  
  12. ;        Only stack-based A-line traps can be called this way,
  13. ;        another mechanism must be used for "normal"
  14. ;        [not in ROM] calls, perhaps through the code
  15. ;        jump table.
  16.  
  17. ;        This will not work on a 68030/040 unless the
  18. ;        instruction pipeline is turned off
  19. ;        (and flushed) before the modified code is
  20. ;        jumped to.  This is now done here and the cache
  21. ;        restored afterwards to minimise the performance
  22. ;        penalty in the Toolbox code...
  23.  
  24. ;        Needless to say, be very, very wary of changing this code.
  25.  
  26.  
  27. ;*******************************************************************************
  28.  
  29.  
  30.  
  31. ;*******************************************************************************
  32. ;
  33. ;        This forms part of the Main Code segment
  34. ;
  35. ;*******************************************************************************
  36.  
  37.         SEG    'Main'
  38.  
  39. ;*******************************************************************************
  40. ;
  41. ;        We need the following external routines
  42. ;
  43. ;*******************************************************************************
  44.  
  45.         IMPORT    SWAPINSTRUCTIONCACHE
  46.         IMPORT    FLUSHINSTRUCTIONCACHE
  47.  
  48.  
  49. ;*******************************************************************************
  50. ;
  51. ;        We export these routines and variables.
  52. ;
  53. ;*******************************************************************************
  54.  
  55.         EXPORT    TOOLBOXTRAP
  56.         EXPORT    trapreg_d0
  57.         EXPORT    trapreg_d1
  58.         EXPORT    trapreg_a0
  59.         EXPORT    trapreg_a1
  60.  
  61. ;*******************************************************************************
  62. ;
  63. ;        Buffers for the trap registers
  64. ;
  65. ;*******************************************************************************
  66.  
  67. trapreg_d0    PROC    EXPORT
  68.         DC.L    $0
  69. trapreg_d1    PROC    EXPORT
  70.         DC.L    $0
  71. trapreg_a0    PROC    EXPORT
  72.         DC.L    $0
  73. trapreg_a1    PROC    EXPORT
  74.         DC.L    $0
  75.  
  76. ;*******************************************************************************
  77. ;
  78. ;        Disable the instruction cache and record the old setting
  79. ;
  80. ;*******************************************************************************
  81.  
  82.  
  83. instrcache    PROC    EXPORT
  84.         DC.W    $0            ; Buffer to record the old ICache setting
  85.  
  86.  
  87. DisableICache    PROC    EXPORT            ; Disable the instruction cache
  88.         JSR    FLUSHINSTRUCTIONCACHE    ; Just in case our loop doesn't fool the cache
  89.         MOVE.W    #0,-(SP)        ; Result word
  90.         MOVE.W    #0,-(SP)        ; Disable the Instruction cache
  91.         JSR    SWAPINSTRUCTIONCACHE    ; ...
  92.         MOVE.L    instrcache,A0        ; Save the previous setting
  93.         MOVE.W    (SP)+,(A0)        ; ...
  94.         RTS
  95.  
  96.  
  97. ;*******************************************************************************
  98. ;
  99. ;        Restore the previous setting of the instruction cache
  100. ;
  101. ;*******************************************************************************
  102.  
  103. RestoreICache    PROC    EXPORT            ; Reset the I/D caches
  104.         MOVE.L    A0,-(SP)        ; push continuation
  105.         MOVE.W    #0,-(SP)        ; Result word
  106.         MOVE.L    instrcache,A0        ; Reset the Instruction cache
  107.         MOVE.W    (A0),-(SP)         ; ...
  108.         BSR    SwapInstructionCache    ; ...
  109.         ADDQ.L    #2,SP            ; Ignore the result
  110.         MOVE.L    trapreg_d0,D0        ; Set registers for register-based
  111.         MOVE.L    trapreg_d1,D1        ;   routines or packages
  112.         MOVE.L    trapreg_a0,A0        ;   ...
  113.         MOVE.L    trapreg_a1,A1        ;   ...
  114.         RTS
  115.  
  116.  
  117.  
  118. ;*******************************************************************************
  119. ;
  120. ;        Trap with short result
  121. ;
  122. ;*******************************************************************************
  123.  
  124. trbuf        PROC    EXPORT            ; Record the trap word.
  125.         TRAP    #$0            ; Dummy trap word
  126.         CLR.L    D0            ; Clear the result
  127.         MOVE.W    (SP),D0            ; Save the short result
  128.         MOVE.L    D0,$12(A6)        ; Record the dummy result
  129.         UNLK    A6            ; Unlink the frame pointer
  130.         MOVE.L    (SP)+,A0        ; Save the return address
  131.         LEA    $0A(SP),SP        ; Pop the arguments
  132.         JMP    (A0)            ; And return        
  133.  
  134.         DC.B $85, 'trbuf'
  135.         DC.W 0
  136.         ENDP    ; trbuf
  137.  
  138.  
  139. ;*******************************************************************************
  140. ;
  141. ;        Trap with longword result
  142. ;
  143. ;*******************************************************************************
  144.  
  145.  
  146. trbuflr        PROC    EXPORT            ; Record the trap word.
  147.         TRAP    #$0            ; Dummy trap word
  148.         MOVE.L    (SP),D0            ; Save the result
  149.         MOVE.L    D0,$12(A6)        ; Record the dummy result
  150.         UNLK    A6            ; Unlink the frame pointer
  151.         MOVE.L    (SP)+,A0        ; Save the return address
  152.         LEA    $0A(SP),SP        ; Pop the arguments
  153.         JMP    (A0)            ; And return        
  154.  
  155.         DC.B $87, 'trbuflr'
  156.         DC.W 0
  157.         ENDP    ; trbuflr
  158.  
  159.  
  160.  
  161.  
  162. ;*******************************************************************************
  163. ;
  164. ;        Trap with no result/result in d0        
  165. ;
  166. ;*******************************************************************************
  167.  
  168.  
  169. trbufnr        PROC    EXPORT            ; Record the trap word.
  170.         TRAP    #$0            ; Dummy trap word
  171.         MOVE.L    D0,$12(A6)        ; Record a dummy result
  172.                         ; or the correct result for register-based routines.
  173.         UNLK    A6            ; Unlink the frame pointer
  174.         MOVE.L    (SP)+,A0        ; Save the return address
  175.         LEA    $0A(SP),SP        ; Pop the arguments
  176.         JMP    (A0)            ; And return        
  177.  
  178.         DC.B $87, 'trbufnr'
  179.         DC.W 0
  180.         ENDP    ; trbufnr
  181.  
  182.  
  183.  
  184. ;*******************************************************************************
  185. ;
  186. ;        Call a Toolbox routine.
  187. ;
  188. ;*******************************************************************************
  189.  
  190.  
  191. TOOLBOXTRAP    PROC    EXPORT
  192.  
  193.         LINK    A6,#0        ; Link for 100% Pascal compatibility
  194.  
  195.         BSR    DisableICache    ; Disable the instruction cache
  196.  
  197.         MOVE.W    $0E(A6),A0
  198.  
  199.  
  200.         MOVE.W    $0C(A6),D1    ; Arg Count -- 128 maximum
  201.         MOVEQ    #0,D0        ; Arg Index -- count UP, since trap args are reversed!    
  202.  
  203.         MOVE.L    $08(A6),A1    ; Parameter buffer
  204.  
  205.         CMP.W    #0,A0        ; Set up the trap and return parameters
  206.         BEQ.B    noresult    ; according to the size of the result
  207.         CMP.W    #1,A0
  208.         BEQ.B    shortresult    
  209.         
  210. longresult    LEA trbuflr,A0        ; Trap Location
  211.         MOVE.W    $10(A6),(A0)    ; Set the Trap
  212.         MOVE.L    #0,-(SP)    ; Longword Result
  213.         JMP    traploop
  214.  
  215. shortresult    LEA trbuf,A0        ; Trap Location
  216.         MOVE.W    $10(A6),(A0)    ; Set the Trap
  217.         MOVE.W    #0,-(SP)    ; Shortword Result
  218.         JMP    traploop
  219.  
  220. noresult    LEA trbufnr,A0        ; Trap Location
  221.         MOVE.W    $10(A6),(A0)    ; Set the Trap
  222.         
  223.  
  224. traploop    CMPI.W    #0,D1        ; when no more args
  225.         BEQ    RestoreICache    ;   do the trap -- this branch is really 
  226.                     ;   a call with nominated continuation in A0!
  227.  
  228.         MOVE.W    (A1,D0),-(SP)    ; push the arg
  229.  
  230.         SUBQ.W    #1,D1        ; Dec. arg count
  231.         ADDQ.W    #2,D0        ; post-increment the index as a BYTE counter
  232.  
  233.         JMP    traploop    ; and repeat.
  234.         
  235.         DC.B $8b, 'ToolboxTrap'
  236.         DC.W 0
  237.         ENDP    ; ToolboxTrap
  238.  
  239.         END
  240.  
  241.